package org.amphiprion.descentv2assistant;

import java.io.File;

import org.amphiprion.descentv2assistant.adapter.MainViewPagerAdapter;
import org.amphiprion.descentv2assistant.dao.db.GameSessionDao;
import org.amphiprion.descentv2assistant.entity.GameSession;
import org.amphiprion.descentv2assistant.ui.fragment.MenuFragment;
import org.amphiprion.descentv2assistant.ui.views.TabPageIndicator;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

public class MainActivity extends SlidingFragmentActivity {
	private ViewPager mPager;
	private MainViewPagerAdapter mPagerAdapter;
	private TabPageIndicator mTabIndicator;
	private MenuFragment mMenuFragment;
	public static MainActivity sThis;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sThis = this;
		setContentView(R.layout.activity_main);

		mPager = (ViewPager) findViewById(R.id.viewPager);
		mTabIndicator = (TabPageIndicator) findViewById(R.id.indicator);

		mPagerAdapter = new MainViewPagerAdapter(this, getSupportFragmentManager());

		// Action bar
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_support_bar));
		// sliding menu
		setSlidingActionBarEnabled(false);
		getSlidingMenu().setBehindOffsetRes(R.dimen.actionbar_home_width);
		getSlidingMenu().setBehindScrollScale(0.5F);
		getSlidingMenu().setTouchModeAbove(0); // TODO 2 pour tablette

		// menu fragment

		FrameLayout frameLayout = new FrameLayout(this);
		frameLayout.setId(R.id.menuRoot);
		setBehindContentView(frameLayout);
		FragmentTransaction localFragmentTransaction = getSupportFragmentManager().beginTransaction();
		this.mMenuFragment = new MenuFragment();
		localFragmentTransaction.add(R.id.menuRoot, this.mMenuFragment);
		localFragmentTransaction.commit();

		// pager`
		mPager.setAdapter(mPagerAdapter);
		mPager.setOffscreenPageLimit(-1 + mPagerAdapter.getCount());

		mTabIndicator.setViewPager(mPager);

		mPager.setCurrentItem(0);

	}

	public void onSearch(View view) {
		onSearchRequested();
	}

	public void onAddGameSession(View view) {
		onAddEditGameSession((GameSession) null);
	}

	public void onAddEditGameSession(final GameSession gameSession) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		// Get the layout inflater
		LayoutInflater inflater = this.getLayoutInflater();

		// Inflate and set the layout for the dialog
		// Pass null as the parent view because its going in the dialog layout
		final View dialogView = inflater.inflate(R.layout.edit_game, null);
		if (gameSession != null) {
			((TextView) dialogView.findViewById(R.id.txtGameName)).setText(gameSession.getName());
			((Spinner) dialogView.findViewById(R.id.cbNumberHeroes)).setSelection(gameSession.getNumberOfHeroes() - 2);
		}
		builder.setView(dialogView).setTitle(R.string.edit_game_title)
		// Add action buttons
				.setPositiveButton(R.string.done, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int id) {
						GameSession gm = gameSession;
						if (gm == null) {
							gm = new GameSession();
						}
						String oldName = gm.getName();
						gm.setName(((TextView) dialogView.findViewById(R.id.txtGameName)).getText().toString());
						gm.setNumberOfHeroes(((Spinner) dialogView.findViewById(R.id.cbNumberHeroes)).getSelectedItemPosition() + 2);

						GameSessionDao.getInstance(MainActivity.this).persist(gm);

						// rename the photo directory (overlord)
						if (gameSession != null) {
							File oldPhotosOLPhoto = new File(Environment.getExternalStorageDirectory() + "/" + ApplicationConstants.DIRECTORY + "/photos/" + oldName.replaceAll("'", "_") + "_" + gameSession.getId());
							File newPhotosOLPhoto = new File(Environment.getExternalStorageDirectory() + "/" + ApplicationConstants.DIRECTORY + "/photos/" + gm.getName().replaceAll("'", "_") + "_" + gameSession.getId());
							oldPhotosOLPhoto.renameTo(newPhotosOLPhoto);
						}
						MenuFragment.sThis.initData();
						// sign in the user ...
					}
				}).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int id) {
						// LoginDialogFragment.this.getDialog().cancel();
					}
				});

		builder.show();

	}

	// @Override
	// public boolean onSearchRequested() {
	// startActivity(new Intent(this, AddSeriesActivity.class));
	// return true;
	// }
	//
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_filter:
			mPagerAdapter.doFilter(mPager.getCurrentItem());
			break;
		case R.id.menu_add_game:
			onAddEditGameSession((GameSession) null);
			break;
		case android.R.id.home:
			toggle();
			break;
		case R.id.main_settings:
			startActivity(new Intent(this, SettingActivity.class));
			break;
		}
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return true;
	}

}
